﻿using SmallJob.Services.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SmallJob.Services.Code;

namespace BackEnd.Controllers
{
     [Authorize]
    public class UserController : Controller
    {
        private readonly IUserService _UserService;
        public UserController(IUserService UserService)
        {
            _UserService = UserService;
        }

        //
        // GET: /User/

        public ActionResult Index(string page,string filter)
        {
            if (string.IsNullOrWhiteSpace(page))
            {
                page = "1";
            }
            ViewData["tableindex"] = new {  page = page, pagesize = Convert.ToInt32(Resources.Resource.ItemPerPage),filter=filter };
            return View();
        }
        public ActionResult Details(int id)
        {
            var model = _UserService.GetById(id);
            return View(model);
        }
        public ActionResult Lock(int id)
        {
            try
            {
                var user = _UserService.GetById(id);
                user.IsLock = true;
                _UserService.Update(user);
                return Json(true);
            }
            catch (Exception)
            {

                return Json(false);
            }
        }
        [HttpPost]
        public ActionResult UnLock(int id)
        {
            try
            {
                var user = _UserService.GetById(id);
                user.IsLock = false;
                _UserService.Update(user);
                return Json(true);
            }
            catch (Exception)
            {

                return Json(false);
            }
        }
        public ActionResult TableUser(int page, int pagesize,string filter)
        {
            int total = 0;          
            pagesize =Convert.ToInt32(Resources.Resource.ItemPerPage);
            if (page == 0)
            {
                page = 1;
            }
            var model = _UserService.GetByPage(page, pagesize, out total);
            if (!string.IsNullOrWhiteSpace(filter))
            {
                model = _UserService.GetByPageFilter(page, pagesize, out total,filter);
                ViewData["filter"] = filter;
            }
            if ((total % pagesize) > 0)
            {
                ViewData["total"] = (total / pagesize) + 1;
            }
            else
            {
                ViewData["total"] = total / pagesize;
            }
            
            ViewData["curpage"] = page;
            ViewData["nextpage"] = page + 1;
            ViewData["prepage"] = page - 1;
            return PartialView(model);
        }
        public ActionResult ResetPassword(int id) { 
            var u = _UserService.GetById(id);
            u.Password = UserUtility.Encrypt("123456",u.Email);
            _UserService.Update(u);
            return Json(true);
        }
    }
}
